package com.qbd.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.qbd.model.Province;
import com.qbd.service.ProvinceService;
import com.qbd.utils.PageBean;
import com.qbd.utils.StringUtil;
import com.qbd.utils.UUIDUtils;

@RestController
@RequestMapping("/province")
public class ProvinceController {

	@Autowired
	private ProvinceService provinceService;

	@RequestMapping("/save")
	public Map<String, Object> saveOrupdate(@ModelAttribute Province province, String id) throws Exception {
		if (id != null) {
			province.setId(id);
			provinceService.updateByPrimaryKeySelective(province);
		} else {
			province.setId(UUIDUtils.getUUIDCode());
			provinceService.insert(province);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("success", "true");
		return map;
	}

	/**
	 * ɾ����¼����ɾ��������¼
	 * 
	 * @param delcaids
	 *            ɾ����id
	 * @param response
	 * @return
	 */
	@RequestMapping("/delete")
	public Map<String, Object> delete(String ids) {
		Map<String, Object> map = new HashMap<String, Object>();
		String arry[] = ids.split(",");
		int j = 0;
		for (int i = 0; i < arry.length; i++) {
			provinceService.deleteByPrimaryKey(arry[i]);
			j++;
		}
		if (j == arry.length) {
			map.put("success", "true");
			map.put("delNums", j);
		} else {
			map.put("errorMsg", "false");
		}
		return map;
	}

	/**
	 * ��ҳ��ʾ���м�¼
	 * 
	 * @param page
	 *            ҳ��
	 * @param rows
	 *            ����
	 * @param response
	 * @param omsProduct
	 *            ��Ʒ����
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public Map<String, Object> list(String page, String rows, @ModelAttribute Province province, String sname)
			throws Exception {
		PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", StringUtil.formatLike(sname));
		map.put("start", pageBean.getStart());
		map.put("size", pageBean.getPageSize());
		List<Province> userList = provinceService.selectByPage(map);
		Long total = provinceService.getTotal(map);
		map.clear();
		map.put("rows", userList);
		map.put("total", total);
		return map;
	}

	@RequestMapping("/provinceAll")
	public List<Map<String, Object>> getAll(String pid) throws Exception {
		List<Province> list = provinceService.selectByPage(null);
		List<Map<String, Object>> list2 = new ArrayList<>();
		for (Province province : list) {
			Map<String, Object> map1 = new HashMap<>();
			map1.put("name", province.getName());
			map1.put("id", province.getId());
			list2.add(map1);
		}
		return list2;
	}
}
